package com.csmht.tododemo.pojo.entity;

import com.csmht.tododemo.pojo.enumeration.RoleEnum;
import com.csmht.tododemo.until.BaseUnit;
import lombok.Builder;
import lombok.Data;

import java.util.LinkedHashMap;
import java.util.Map;

@Data
@Builder
public class Role {

    private Long id;

    private RoleEnum role;

    public Role(Long id, RoleEnum roleStr) {
        this.id = id;
        this.role = roleStr;
    }

    private Role(Long id, String role, BaseUnit baseUnit) {}


    public static Role convertToRole(Object obj) {
        switch (obj) {
            case null -> throw new IllegalArgumentException("待转换对象不能为null");

            case Role role1 -> {
                return role1;
            }
            case LinkedHashMap map ->{
                Long id = ((Integer) map.get("id")).longValue();


                String roleStr = (String) map.get("role");

                RoleEnum roleEnum = RoleEnum.valueOf(roleStr);

                return new Role(id, roleEnum);
            }
            default -> {
            }
        }


        // 其他无法转换的类型
        throw new IllegalArgumentException("不支持的转换类型: " + obj.getClass().getName());
    }

}
